<!DOCTYPE html>
<html>

<head>
    <title>我的消息</title>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <script src="/js/jquery/jquery.min.js"></script>
    <link rel="stylesheet" href="/js/bootstrap/css/bootstrap.css">
    <script src="/js/bootstrap/js/bootstrap.js"></script>
    <link rel="stylesheet" href="/css/font/css/font-awesome.min.css"/>
    <link rel="stylesheet" href="/css/reset.css"/>
    <link rel="stylesheet" href="/css/message.css"/>
    <script src="/js/plugins/jrender/jrender.js"></script>
    <script src="/js/common.js"></script>
    <link rel="stylesheet" href="/js/plugins/dialog/dialog.css">
    <script src="/js/plugins/dialog/dialog.min.js"></script>
    <script src="/js/plugins/jquery-form/jquery.form.js"></script>
    <script>
        $(function () {
            var userId = getParams().userId;
            var senderId = getParams().senderId;
            var msg;

            /*渲染发件人名字*/
            $.get("/users", {id: senderId}, function (data) {
                $("#senderName").html(data.nickName);
            });

            /*查询所有消息 */
            $.get("/privateMessages/" + senderId + "/" + userId, function (data) {
                msg = data.list;
                renderView($(".msg"), {list: msg});
            });

            /*标记已读*/
            $.post({
                url: "/privateMessages",
                type: "PUT",
                data: {receiverId: userId, senderId: senderId}
            });


            /*回复消息*/
            $(".submitBtn").click(function () {
                var content = $("#content").val();
                $.post("/privateMessages", {
                    "sender.id": userId,
                    receiverId: senderId,
                    content: content
                }, function (data) {
                    if (data.success) {
                        $("#content").val("");
                        location.reload();
                    }
                });
            });

            /*渲染界面函数*/
            function renderView(ele, data) {
                ele.renderValues(data, {
                    handle: function (ele, value) {
                        ele.href = "/userProfiles.html?id=" + value;
                    },
                    viewHandle: function (ele, value) {
                        if (value == userId) {
                            $(ele).attr("align", "right");
                        }
                    }
                });
            }
        })
    </script>

</head>

<body>
<div class="search-head">
    <div class="row nav-search">
        <div class="col">
            <a href="javascript:history.go(-1);">
                <span><i class="fa fa-chevron-left"></i></span>
            </a>
        </div>
        <div class="col">
            <span>与&nbsp;&nbsp;</span>
            <span id="senderName" style="color: #ff8a00"></span>
            <span>&nbsp;&nbsp;的对话</span>
        </div>
        <div class="col"></div>
    </div>
</div>


<!--渲染消息-->
<div class="container msg">
    <dl class="itemList" render-loop="list">
        <div render-key="list.sender.id" render-fun="viewHandle">
            <dd render-attr="mid=list.id">
                <p><a render-key="list.sender.id" render-fun="handle"><img class="rounded-circle" width=40px
                                                                           height="40px"
                                                                           render-src="list.sender.headImgUrl">
                    <span render-html="list.sender.nickName" style="color:  #ff8a00"></span></a>
                    <span>说:</span>
                </p>
                <p render-html="list.content"></p>
                <p><span render-html="list.sendTime"></span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>
                </p>
            </dd>
        </div>
    </dl>
</div>

<dl>
    <dd class="itemList">
        <form id="sendForm" method="post">
            <textarea style="width: 100%" id="content"></textarea>
            <button type="button" class="btn submitBtn" style="color:  #ff8a00;float: right">回复</button>
        </form>
    </dd>
</dl>


</body>

</html>